package com.maibow.util;

import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.List;

public class PrinterAction {
    // private PrinterMessage printerMessage;
    // private List<PrinterMessage> list;
    public static void main(String[] args) {
        List<PrinterMessage> list = new ArrayList<PrinterMessage>();
        PrinterMessage bean1 = new PrinterMessage();
        bean1.setOrderNo("110");
        bean1.setAdmin("余书慧");
        bean1.setBrand("寿师傅时尚日本料理");
        bean1.setTotalPrice(60.00);
        bean1.setShop("蓝色港湾店");
        bean1.setTableName("A2");
        bean1.setTeaPrice(2.00);
        bean1.setCash("0.00");
        bean1.setCup("建设银行");
        bean1.setPaidIn(50.00);
        bean1.setPerNum(8);
        bean1.setPrice(15.20);
        bean1.setQty(1);
        bean1.setItemName("蛋黄蝶");
        System.out.println("========bean1:" + bean1);
        list.add(0, bean1);
        PrinterMessage bean2 = new PrinterMessage();
        bean2.setOrderNo("110");
        bean2.setAdmin("余书慧");
        bean2.setBrand("寿师傅时尚日本料理");
        bean2.setTotalPrice(60.00);
        bean2.setShop("蓝色港湾店");
        bean2.setTableName("A2");
        bean2.setTeaPrice(2.00);
        bean2.setCash("0.00");
        bean2.setCup("建设银行");
        bean2.setPaidIn(50.00);
        bean2.setPerNum(8);
        bean2.setPrice(15.20);
        bean2.setQty(1);
        bean2.setItemName("黄蝶");
        list.add(1, bean2);
        PrinterMessage bean3 = new PrinterMessage();
        bean3.setOrderNo("110");
        bean3.setAdmin("余书慧");
        bean3.setBrand("寿师傅时尚日本料理");
        bean3.setTotalPrice(60.00);
        bean3.setShop("蓝色港湾店");
        bean3.setTableName("A2");
        bean3.setTeaPrice(2.00);
        bean3.setCash("0.00");
        bean3.setCup("建设银行");
        bean3.setPaidIn(50.00);
        bean3.setPerNum(8);
        bean3.setPrice(16.20);
        bean3.setQty(1);
        bean3.setItemName("红蝶");
        list.add(2, bean3);
        PrinterMessage bean4 = new PrinterMessage();
        bean4.setOrderNo("110");
        bean4.setAdmin("余书慧");
        bean4.setBrand("寿师傅时尚日本料理");
        bean4.setTotalPrice(60.00);
        bean4.setShop("蓝色港湾店");
        bean4.setTableName("A2");
        bean4.setTeaPrice(2.00);
        bean4.setCash("0.00");
        bean4.setCup("建设银行");
        bean4.setPaidIn(50.00);
        bean4.setPerNum(8);
        bean4.setPrice(17.20);
        bean4.setQty(1);
        bean4.setItemName("绿蝶");
        list.add(3, bean4);
        PrinterMessage bean5 = new PrinterMessage();
        bean5.setOrderNo("110");
        bean5.setAdmin("余书慧");
        bean5.setBrand("寿师傅时尚日本料理");
        bean5.setTotalPrice(60.00);
        bean5.setShop("蓝色港湾店");
        bean5.setTableName("A2");
        bean5.setTeaPrice(2.00);
        bean5.setCash("0.00");
        bean5.setCup("建设银行");
        bean5.setPaidIn(50.00);
        bean5.setPerNum(8);
        bean5.setPrice(18.20);
        bean5.setQty(1);
        bean5.setItemName("黄蝶");
        list.add(4, bean5);
        PrinterMessage bean6 = new PrinterMessage();
        bean6.setOrderNo("110");
        bean6.setAdmin("余书慧");
        bean6.setBrand("寿师傅时尚日本料理");
        bean6.setTotalPrice(60.00);
        bean6.setShop("蓝色港湾店");
        bean6.setTableName("A2");
        bean6.setTeaPrice(2.00);
        bean6.setCash("0.00");
        bean6.setCup("建设银行");
        bean6.setPaidIn(50.00);
        bean6.setPerNum(8);
        bean6.setPrice(19.20);
        bean6.setQty(1);
        bean6.setItemName("紫蝶");
        list.add(5, bean6);
        PrinterMessage bean7 = new PrinterMessage();
        bean7.setOrderNo("110");
        bean7.setAdmin("余书慧");
        bean7.setBrand("寿师傅时尚日本料理");
        bean7.setTotalPrice(60.00);
        bean7.setShop("蓝色港湾店");
        bean7.setTableName("A2");
        bean7.setTeaPrice(2.00);
        bean7.setCash("0.00");
        bean7.setCup("建设银行");
        bean7.setPaidIn(50.00);
        bean7.setPerNum(8);
        bean7.setPrice(150.20);
        bean7.setQty(1);
        bean7.setItemName("白蝶");
        list.add(6, bean7);
        System.out.println("========list:" + list);
        print(list);
    }

    /**
     * 用于将商品零售进行进行打印
     * 
     */
    public static void print(List<PrinterMessage> list) {
        // 通俗理解就是书、文档
        Book book = new Book();
        // 设置成竖打
        PageFormat pf = new PageFormat();
        pf.setOrientation(PageFormat.PORTRAIT); // LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白
        // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
        Paper p = new Paper();
        int length = printSize(list);
        System.out.println("=========length:" + length);
        p.setSize(200, length); // 纸张大小(590,
                                // 840)表示A4纸
        p.setImageableArea(10, 10, 227, length); // A4(595
                                                 // X
        // 842)设置打印区域，其实0，0应该是72，72
        // ，因为A4纸的默认X,Y边距是72
        pf.setPaper(p);
        // // 把 PageFormat 和 Printable
        // 添加到书中，组成一个页面
        book.append(new PrinterDao(list), pf);
        // // 获取打印服务对象
        PrinterJob job = PrinterJob.getPrinterJob();
        // // 设置打印类
        job.setPageable(book);
        try {
            // //
            // 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
            // boolean a = job.printDialog();
            // if (a) {
            job.print();
            // }
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }

    public static Integer printSize(List<PrinterMessage> list) {
        int k = 20;
        if (list.size() != 0) {
            k = k * list.size();
        }
        return 160 + k;
    }
}